package com.nondo.dean.product.service;



import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.nondo.dean.common.tool.StringUtils;
import com.nondo.dean.product.domain.ProductType;
import com.nondo.dean.product.domain.ProductTypeExample;
import com.nondo.dean.product.mapper.ProductTypeMapper;
import com.nondo.dean.product.model.ProductTypeBean;

/**
 * @author xiaofan
 * @Description: ${todo}
 * @throws
 * @date 2018-03-2816:28
 */
@Component
public class ProductTypeService {

    @Autowired
    private ProductTypeMapper  productTypeMapper;

    /**
     * 检查产品类型是否存在重复数据
     * @return
     */
    public List<ProductType> checkDuplicate(ProductType productType ){
        //检查是否存在重复的数据
        ProductTypeExample productTypeCheck = new ProductTypeExample();
        productTypeCheck.createCriteria().andCodeEqualTo(productType.getCode());

        return this.productTypeMapper.selectByExample(productTypeCheck);
    }

    /**
     * 同步产品类型数据
     *
     * @param list
     * @return
     */
    public int synchronousProductData(List<ProductType> list) {
        int count=0;
        try {
            for(ProductType productType : list) {
                List<ProductType> productTypesExist=checkDuplicate(productType);

                if (!productTypesExist.isEmpty()) {
                    ProductTypeExample productTypeExample= new ProductTypeExample();
                    //where条件设置
                    productTypeExample.createCriteria().andCodeEqualTo(productType.getCode());
                    //执行修改
                    count+= productTypeMapper.updateByExampleSelective(productType,productTypeExample);
                }else{
                    //新增产品数据
                    count+= productTypeMapper.insertSelective(productType);
                }
            }
        } catch (Exception e) {
            return 0;
        }
        return count;
    }

    /**
     * 插入产品类型
     * @param productType
     * @return
     */
    @Transactional
    public int addProductType(ProductType productType) {
        return productTypeMapper.insertSelective(productType);
    }

    /**
     * 修改产品类型
     * @param productType
     * @return
     */
    @Transactional
    public int updateProductType(ProductType productType) {
        return productTypeMapper.updateByPrimaryKeySelective(productType);
    }

    /**
     * 按照主键ID查询产品类型
     * @param id
     * @return
     */
    public ProductType selectProductType(Integer id) {
        return productTypeMapper.selectByPrimaryKey(id);
    }

    /**
     * 产品类型分页查询列表
     * @param offset
     * @param limit
     * @param productType
     * @return
     */
    public List<ProductType> getProductTypeList(int offset, int limit, ProductType productType) {
        ProductTypeExample example = new ProductTypeExample();
        example.setOrderByClause("create_date DESC");
        RowBounds rowBounds = new RowBounds(offset, limit);
        ProductTypeExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, productType);
        return productTypeMapper.selectByExampleWithRowbounds(example, rowBounds);
    }

    /**
     * 产品类型总数统计
     * @param productType
     * @return
     */
    public Long count(ProductType productType) {
        ProductTypeExample example = new ProductTypeExample();
        ProductTypeExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, productType);
        return productTypeMapper.countByExample(example);
    }

    /**
     * 组装查询条件
     * @param criteria
     * @param productType
     */
    void setCriteria(ProductTypeExample.Criteria criteria, ProductType productType) {
        if (productType != null) {
            if (productType.getId() != null)
                criteria.andIdEqualTo(productType.getId());
            if (StringUtils.isNoneBlank(productType.getName()))
                criteria.andNameLike("%" + productType.getName() + "%");
        }
        criteria.andDelFlagEqualTo(String.valueOf(0));
    }

   /**
    * 按照产品类型名称查询产品类型
    * @param name
    * @return
    */
    public ProductType selectProductTypeByName(String name) {
        ProductTypeExample example = new ProductTypeExample();
        ProductTypeExample.Criteria criteria = example.createCriteria();
        criteria.andNameEqualTo(name);
        criteria.andDelFlagEqualTo(String.valueOf(0));
        List<ProductType> productTypeList = productTypeMapper.selectByExample(example);
        return CollectionUtils.isNotEmpty(productTypeList)?productTypeList.get(0):null;
    }
    /**
     * 查询所有物料类型
     * @return
     */
    public HashMap<String, ProductType> selectProductTypeAll(){
        HashMap<String, ProductType> productTypeMap=new HashMap<String, ProductType>();
        ProductTypeExample example = new ProductTypeExample();
        ProductTypeExample.Criteria criteria = example.createCriteria();
        criteria.andDelFlagEqualTo(String.valueOf(0));
        List<ProductType> list= productTypeMapper.selectByExample(example);
        for(ProductType pr : list){
            ProductType productType=new ProductType();
            productType.setId(pr.getId());
            productType.setCode(pr.getCode());
            productType.setName(pr.getName());
            productTypeMap.put(pr.getCode(),productType);
        }


        return productTypeMap;
    }

    /**
     * 获取所有产品类型列表
     * @param request
     * @param response
     * @return List<{@link ProductTypeBean}>
     */
    public List<ProductTypeBean> getProductTypeList() {
        ProductTypeExample example = new ProductTypeExample();
        ProductTypeExample.Criteria criteria = example.createCriteria();
        criteria.andDelFlagEqualTo(String.valueOf(0));
        List<ProductType> productTypeList = productTypeMapper.selectByExample(example);
        List<ProductTypeBean> productTypeBeanList = new ArrayList<ProductTypeBean>();
        for (ProductType productType : productTypeList) {
            ProductTypeBean productTypeBean = ProductTypeBean.covertToBean(productType);
            productTypeBeanList.add(productTypeBean);
        }
       return productTypeBeanList;
    }
}
